<?php

/*
 * litchicms
 * Copyright (C) 2010-5-1 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

class mysql {

    private $querynum = 0;
    private $charset = 'utf8';
    private $link_identifier = null;

    public function __construct($dbhost, $dbuser, $dbpw, $preifx="brk_", $dbname = '', $pconnect = 0, $charset='utf8') {
        if ($pconnect) {
            if (!$this->link_identifier = mysql_pconnect($dbhost, $dbuser, $dbpw)) {
                $this->halt("连接数据库失败");
            }
        } else {
            if (!$this->link_identifier = mysql_connect($dbhost, $dbuser, $dbpw)) {
                $this->halt("连接数据库失败");
            }
        }

        $version = $this->version();
        if ($version > '4.1') {
            if (!empty($this->charset)) {
                mysql_query('SET character_set_connection=' . $this->charset . ', character_set_results=' . $this->charset . ', character_set_client=binary', $this->link_identifier);
            }
            if ($version > '5.0.1') {
                mysql_query("SET sql_mode=''", $this->link_identifier);
            }
        }

        if ($dbname) {
            mysql_select_db($dbname, $this->link_identifier);
        }

        $this->query_unbuff("SET NAMES $charset");
    }

    public function select_db($dbname) {
        return mysql_select_db($dbname, $this->link_identifier);
    }

    public function fetch_array($query, $result_type = MYSQL_ASSOC) {
        return mysql_fetch_array($query, $result_type);
    }

    public function query($sql) {
        $query = mysql_query($sql, $this->link_identifier);
        if (DEBUG) {
            global $BRK_GLOBAL;
            if (empty($_SGLOBAL['debug']))
                $_SGLOBAL['debug'] = array('SQL' => array());
            $time = microtime(TRUE);
            $explain = array();
            $info = mysql_info();
            if (strtolower(substr(trim($sql), 0, 7)) == 'select ') {
                $explain = mysql_fetch_assoc(mysql_query('EXPLAIN ' . $sql, $this->link_identifier));
            }
            $_SGLOBAL['debug']['SQL'][] = array('sql' => $sql, 'time' => $time, 'info' => $info, 'explain' => $explain);
        }
        if (!$query) {
            brk_log(0, $sql . "语句执行失败");
        }
        $this->querynum++;

        return $query;
    }

    public function query_unbuff($sql) {
        if (function_exists("mysql_unbuffered_query")) {
            mysql_unbuffered_query($sql, $this->link_identifier);
        } else {
            $this->query($sql);
        }
    }

    public function affected_rows() {
        return mysql_affected_rows($this->link_identifier);
    }

    public function error() {
        return (($this->link_identifier) ? mysql_error($this->link_identifier) : mysql_error());
    }

    public function errno() {
        return intval(($this->link_identifier) ? mysql_errno($this->link_identifier) : mysql_errno());
    }

    public function result($query, $row) {
        $query = @mysql_result($query, $row);
        return $query;
    }

    public function num_rows($query) {
        $query = mysql_num_rows($query);
        return $query;
    }

    public function num_fields($query) {
        return mysql_num_fields($query);
    }

    public function free_result($query) {
        return mysql_free_result($query);
    }

    public function insert_id() {
        return ($id = mysql_insert_id($this->link_identifier)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
    }

    public function fetch_row($query) {
        $query = mysql_fetch_row($query);
        return $query;
    }

    public function version() {
        return mysql_get_server_info($this->link_identifier);
    }

    public function close() {
        return mysql_close($this->link_identifier);
    }

    public function setchar($char) {
        $this->query_unbuff("SET NAMES $char");
    }

}
?>
